Skip to content

Expressions container - #882

Open
brynpickering wants to merge 5 commits into
masterfrom
feat/expression-container
Open

Expressions container#882
brynpickering wants to merge 5 commits into
masterfrom
feat/expression-container

Conversation

@brynpickering

Copy link
Copy Markdown
Contributor

Partially closes #881

Note

Initial implementation in #871 was human-generated. I've cherry-picked that and then iterated on it with Claude to close some implementation gaps and to add tests/docs.

Note

This does not address the concept of lazy expressions from #881 as I wanted to keep this PR lightweight and less controversial.

Changes proposed in this Pull Request

Model.add_expressions registers a LinearExpression or QuadraticExpression under a name (auto-generated as expr0, expr1, ... if omitted), accessible afterwards via Model.expressions (an Expressions container mirroring Model.variables/Model.constraints) and removable via Model.remove_expressions.
Named expressions are persisted by Model.to_netcdf/linopy.read_netcdf and preserved by Model.copy, copy.copy, copy.deepcopy, and pickling.

Checklist

  • AI-generated content is marked (see AGENTS.md).
  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@FabianHofmann FabianHofmann left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is extremely non-controversial. let's pull this in. Just to test potential future regressions let's also catch the case that returned and stored expression are the same (as suggested below)

Comment thread test/test_expressions.py

expr = m.add_expressions(x, name="from_var")
assert isinstance(expr, LinearExpression)
assert_linequal(expr, x.to_linexpr())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_linequal(expr, x.to_linexpr())
assert_linequal(expr, x.to_linexpr())
assert_linequal(expr, m.expressions["from_var"])

Comment thread test/test_expressions.py

expr = m.add_expressions([(2, x)], name="from_tuples")
assert isinstance(expr, LinearExpression)
assert_linequal(expr, 2 * x)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert_linequal(expr, 2 * x)
assert_linequal(expr, 2 * x)
assert_linequal(expr, m.expressions["from_tuples"])

@codspeed-hq

codspeed-hq Bot commented Aug 10, 2026

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 3 improved benchmarks
❌ 1 regressed benchmark
✅ 171 untouched benchmarks
⏩ 175 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory test_to_lp[nodal_balance-severity=0] 2.8 MB 3.3 MB -15.43%
Memory test_to_lp[masked-n=100] 2.7 MB 1.4 MB +83.39%
Memory test_to_lp[storage-n=250] 35.8 MB 29.4 MB +21.97%
Memory test_to_lp[rolling-severity=0] 3.1 MB 2.7 MB +14.37%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/expression-container (8461fcb) with master (0f6cdad)

Open in CodSpeed

Footnotes

  1. 175 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attach expressions (incl. lazy ones) to linopy.Model

2 participants